package cn.edu.cumt.ec.demo.controller;

import cn.edu.cumt.ec.demo.entity.Result;
import cn.edu.cumt.ec.demo.entity.User;
import cn.edu.cumt.ec.demo.enums.MessageEnum;
import cn.edu.cumt.ec.demo.service.IUserService;
import cn.edu.cumt.ec.demo.utils.ResultUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * createAt: 2020/01/05
 */

@RestController
@Api(value="User API接口")
@RequestMapping("/api/v1/users")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping(value = "")
    @ApiOperation(value = "列表", notes = "分页查询")
    public Result<Page<User>> listPage(@RequestParam("startPage") long startPage, @RequestParam("pageSize") long pageSize){
        Page<User> page=new Page<>(startPage,pageSize);
        Page<User> users=userService.page(page);
        return ResultUtil.success(users);
    }

    @PostMapping(value = "")
    @ApiOperation(value = "添加：单个", notes = "添加：单个")
    public Result<Page<User>> insert(@RequestBody User user){
        if(userService.save(user)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(MessageEnum.USER_EXIST.getCode(),MessageEnum.USER_EXIST.getMessage());
        }
    }
    
    @DeleteMapping(value = "")
    @ApiOperation(value = "删除：单个", notes = "删除：单个")
    public Result<Page<User>> deleteById(Long id){
        if(userService.removeById(id)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(MessageEnum.USER_EXIST.getCode(),MessageEnum.USER_EXIST.getMessage());
        }
    }
}
